<?php
namespace Common\Utils;
class DsRkxx{
	public $nead_row	=	array(
								'nsrsbh'=>array('纳税人识别号','nsrsbh'),
								'nsrmc'=>array('纳税人名称','nsrmc'),
								'zsxm'=>array('征收项目','zsxm'),
								'se'=>array('税额','se'),
								'rkrq'=>array('入库日期','rkrq'),
							);
	public $file_path="dsrkxx/";

	public $_err="";

	public function __construct()
	{
		$this->file_path=C("excel_file_path").$this->file_path;
	}


	/**
	 * 将文件夹下的所有文件都导入到mysql中
	 * @return [type] [description]
	 */
	public function moreFileToMysql()
	{
		$files=scandir($this->file_path);
		foreach($files as $file)
		{
			if($this->oneFileToMysql($file))
			{
				echo "\r\n <=======文件:$file 数据导入完成======>\r\n";
			}
		}
	}

/**
 * 从一个csv文件中把数据导入到mysql中
 * @param  [type] $file_name [description]
 * @return [type]            [description]
 */
	public function oneFileToMysql($file_name)
	{
		
		$md_comlist=D("ComList");
		$md_dsrkxx=D("DsRkxx");
		$md_sszsxx=D("CzjSszsxx");

		$file=$this->file_path.$file_name;

		$group_key=md5_file($file);


		if(!file_exists($file))
		{
			echo "$file 文件不存在!";
			return false;
		}

		$f = fopen($file,"r");
		$i=0;
		while(! feof($f))
		{
			$row=fgetcsv($f);
			$row=array_map(array($this, 'iconv_str'), $row);

			$i++;
			if($i==1)
			{
				//解析表头
				$row_key=$this->findRowKey($row);
				continue;
			}

			if(!is_array($row_key))
			{
				return false;
			}

			$data_row=array(
				'row_number'=>$i,
				'group_key'=>$group_key,
			);
			//表头处理完毕.开始处理内容
			foreach($row_key as $key=>$row_num)
			{
				$data_row[$key]=$row[$row_num];
			}

			if(empty($data_row['nsrsbh']) && empty($data_row['nsrmc'])) continue;

			$data_row['csv_data']=serialize($row);

			//先记录企业信息
			$md_comlist->add_new($data_row['nsrsbh'],$data_row['nsrmc']);


			$state=$md_dsrkxx->addNewData($data_row);
			if($state!=false)
			{
				//echo "第 $i 行数据添加完成!\r\n";
				//插入到MsSQL当中去
				if($state['state'] == $md_dsrkxx::STATE_1)
				{
					$aa=$md_sszsxx->addNewRow(1,$state);
					$md_dsrkxx->setOver($state['id'],$aa);
				}
			}
			else
			{
				echo "第 $i 行数据添加失败!\r\n";
			}
			unset($row);
		}
		fclose($f);
		unset($f);
		unset($md_dsrkxx);
		unset($md_comlist);
		return true;
	}

	/**
	 * 根据表头查询各内容列的列号
	 * @param  [type] $one_row [description]
	 * @return [type]          [description]
	 */
	protected function findRowKey($one_row)
	{
		$row_key=array();
		foreach($one_row as $key=>$v)
		{
			
			$v=strtolower($v);
			foreach($this->nead_row as $nead_key=>$nead_v)
			{
				if(in_array($v, $nead_v))
				{
					$row_key[$nead_key]=$key;
					continue;
				}

			}
		}

		if(count($row_key)==count($this->nead_row))
		{
			return $row_key;
		}
		else
		{
			$this->_err="数据列不全";
			return false;
		}


		
		
		//print_r($row_key);
				
		//die();
	}

	/**
	 * 字符集转换
	 * @param  [type] $str [description]
	 * @return [type]      [description]
	 */
	protected function iconv_str($str)
	{
		return iconv("GBK","UTF-8",$str);
	}

}